﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CorsatAdminApp.Library.TempTaskClasses
{
    class DataManger
    {


        #region private variable

        private static SqlConnection _Connection = new SqlConnection("SERVER=.;DATABASE=Corsat_db;Integrated Security=True");
        private static SqlCommand _Command = new SqlCommand("", _Connection);

        private static SqlDataAdapter _Adapter = new SqlDataAdapter(_Command);
        private static DataSet _DataSet = new DataSet();
        private static DataTable _DataTable = new DataTable();


        #endregion

        #region private Method

        private static void OpenConnection()
        {
            if (_Connection.State != ConnectionState.Open)
            {
                _Connection.Open();
            }
        }

        private static void CloseConnection()
        {
            if (_Connection.State != ConnectionState.Closed)
            {
                _Connection.Close();
            }
        }

        #endregion

        #region Public Method


        public static DataTable SelectAbsence(int CourseTitleId ,int CourseLevel ,int Year)
        {

            _Adapter.SelectCommand.CommandText = "SelectAbsence";
            _Adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            _Adapter.SelectCommand.Parameters.Clear();


            _Adapter.SelectCommand.Parameters.Add("@CourseTitleId", SqlDbType.Int).Value = CourseTitleId;
            _Adapter.SelectCommand.Parameters.Add("@CourseLevel", SqlDbType.Int).Value = CourseLevel;
            _Adapter.SelectCommand.Parameters.Add("@Year", SqlDbType.Int).Value = Year;
           
            _DataTable = new DataTable();
            _Adapter.Fill(_DataTable);

            return _DataTable;
          
            
        }

        public static List<Item> SelectCourses()
        {

            List<Item> Coursetitle = new List<Item>();

            for (int i = 0; i < 4; i++)
            {
                Item x = new Item();
                x.ItemId = i;
                x.ItemName = "x" + i;
                Coursetitle.Add(x);
            }

            return Coursetitle;
        }

        #endregion

        public static void UpdateAbsence(Absence_Administration absence)
        {
            _Command.CommandText = "UpdateAbsence";
            _Command.CommandType = CommandType.StoredProcedure;

            _Command.Parameters.Clear();


            _Command.Parameters.Add("@CourseTitleId", SqlDbType.Int).Value = absence.CourseTitle.ItemId;
            _Command.Parameters.Add("@CourseLevel", SqlDbType.Int).Value = absence.CourseLevel;
            _Command.Parameters.Add("@Year", SqlDbType.Int).Value = absence.CourseLevel;

            for (int i = 0; i < absence.Absence.Rows.Count; i++)
            {

                _Command.Parameters.Add("@AccountId", SqlDbType.Int).Value = absence.Absence.Rows[i]["AccountId"];
                _Command.Parameters.Add("@Att_1", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_1"];
                _Command.Parameters.Add("@Att_2", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_2"];
                _Command.Parameters.Add("@Att_3", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_3"];
                _Command.Parameters.Add("@Att_4", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_4"];
                _Command.Parameters.Add("@Att_5", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_5"];
                _Command.Parameters.Add("@Att_6", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_6"];
                _Command.Parameters.Add("@Att_7", SqlDbType.Int).Value = absence.Absence.Rows[i]["@Att_7"];

            }


            OpenConnection();
           _Command.ExecuteNonQuery();
            CloseConnection();
        }

        public static void UpdateAccountResult(Result result)
        {
            MessageBox.Show("Update complete");
        }

        public static void SaveConference(Conference conference)
        {
            throw new NotImplementedException();
        }
    }
}
